
function TreeNode(val) {
    this.val = val
    this.left = null
    this.right = null
}
function buildTree(preorder, inorder) {
    function createTree(preorder, inorder, n) {
        if (n === 0) {
            return null
        }
        let k = 0
        while (preorder[0] !== inorder[k]) {
            k += 1
        }
        let node = new TreeNode(inorder[k])
        node.left = createTree(preorder.slice(1, k + 1), inorder.slice(0, k), k)
        node.right = createTree(preorder.slice(k + 1), inorder.slice(k + 1), n - k - 1)
        return node
    }
    return createTree(preorder, inorder, inorder.length)
}

let preorder = ['A', 'B', 'D', 'E', 'C', 'F']
let inorder = ['D', 'B', 'E', 'A', 'C', 'F']
console.log(buildTree(preorder, inorder));